package com.leenmvc.core.utils.thread.runnables;

import com.leenmvc.core.cache.CacheFactory;
import com.leenmvc.core.enums.CacheType;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * 这个线程会一直在后台运行，持续地刷新redis的状态
 * 是否可连接的状态
 */
public class RefreshRedisStatus implements Runnable {

    @Override
    public void run() {

        List<CacheType> redisItems = CacheType.getRedisItem();
//        while (true) {
            for (CacheType type : redisItems) {
                try {
                    Jedis jedis = new Jedis(type.getHost(), type.getPort());
                    String ping = jedis.ping();
                    if (ping.equalsIgnoreCase("PONG")) {
                        CacheFactory.put(CacheType.STATUS, "redisStatus:" + type.getHost(), 1);
                    }
                } catch (Exception e) {
                    CacheFactory.put(CacheType.STATUS, "redisStatus:" + type.getHost(), 0);
                }
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//        }

    }

}
